package com.own.demo.格子合并;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.geogrid.BoundingBoxUtil;
import com.own.demo.geogrid.UtilsGeoGrid;
import com.own.demo.utils.DateUtils;
import com.own.demo.utils.FileToStringUtils;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.vo.Bounds;
import com.own.demo.长沙空中路网格子.VMainParam;
import com.own.demo.长沙空中路网格子.添加路网ALL;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;
import org.opengis.geometry.BoundingBox;

import java.io.File;
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    //    double sideLen = 0.0069 0.0029  0.0299 0.0299 ;
    static double sideLenV1=200;

    public static ExecutorService executorService =newFixedThreadPool(10);

    public static void main(String[] args) throws ParseException {
        Date sTime=new Date();
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----全局解析数据完成："+(DateUtils.formatTime(sTime,new Date())));
        VMainParam vMainParam= vMainParam(sideLenV1);
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----公共参数完成："+(DateUtils.formatTime(sTime,new Date())));
        sTime=new Date();

        添加路网ALL.goGeoJSONV2Local(vMainParam, true);
        System.out.println(DateUtils.parseDateToStr(new Date())+"-----路网数据："+(DateUtils.formatTime(sTime,new Date())));
        System.exit(-1);
    }

    public static  VMainParam vMainParam(double sidelenSize){
        System.out.println("开始执行");
        File geoZJ = new File("D:\\dkSpace\\油库规划\\路网\\障碍物\\可以规划的区域.geojson");
        String geoData = FileToStringUtils.txt2String(geoZJ);
        JSONObject jsonObject = JSONObject.parseObject(geoData);

        JSONArray jsonArray = jsonObject.getJSONArray("features");


//        String[] adasStr = geoData.split("\\,");
//        int row = adasStr.length;
//        System.out.println("行政区域围栏点数量：" + row);
//        BuildingSelect.init("E:\\ownCode\\emg-geojson\\src\\test\\java\\com\\own\\demo\\长沙空中路网格子\\building-40-up.json");

        List<Double> lonList = new ArrayList(100000);
        List<Double> latList = new ArrayList(100000);

        int row=jsonArray.size();

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < row; i++) {
            JSONArray jsonObject1=jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates");
            for (int j = 0; j < jsonObject1.size(); j++) {
                JSONArray tempV2=jsonObject1.getJSONArray(j);
                for (int k = 0; k < tempV2.size(); k++){
//                    for (int l = 0; l < tempV2.getJSONArray(k).size(); l++){
                        JSONArray point = tempV2.getJSONArray(k);
                        lonList.add(point.getDouble(0));
                        latList.add(point.getDouble(1));
//                    }
                    stringBuffer.append(point.getDouble(0)+" "+point.getDouble(1)+",");
                }
            }
        }

        String buildingStr = stringBuffer.deleteCharAt(stringBuffer.length()-1).toString();
        System.out.println("lon,lat：" + buildingStr);


        System.out.println("lon长度" + lonList.size());
        System.out.println("lat长度" + latList.size());

        double minLon = Collections.min(lonList);
        double maxLon = Collections.max(lonList);
        double minLat = Collections.min(latList);
        double maxLat = Collections.max(latList);
        Bounds bounds = new Bounds(minLon, maxLon, minLat, maxLat);
        try {


            BoundingBox boundingBox = BoundingBoxUtil.getBoundingBox(minLon, maxLon, minLat, maxLat);
            Geometry geometry = PoiTypeEnums.geometry(buildingStr, PoiTypeEnums.POLYGON.getCode());
//            GeometryJSON gjson = new GeometryJSON();
//            StringReader reader = new StringReader(geoData);
//
//
//            // 读取 GeoJSON 转换为 Geometry
//            Geometry geometry = gjson.read(reader);

            System.out.println("对角线边数：" + sidelenSize);
            double sideLen =0.0007;
            List<Geometry> geometries = new UtilsGeoGrid().gridGeometryInfo(geometry, boundingBox, true, sideLen);
            return new VMainParam(bounds,geometry,geometries);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static ExecutorService newFixedThreadPool(int nThreads) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        return executor;
    }
}
